package com.mentaleye.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.DeptBean;
import com.mentaleye.bean.DeptRelationBean;
import com.mentaleye.bean.TeacherBean;
import com.mentaleye.biz.DeptBiz;
import com.mentaleye.biz.DeptRelationBiz;
import com.mentaleye.biz.TeacherBiz;
import com.mentaleye.util.CommonPage;

@Controller
@RequestMapping(value = "/deptrelation")
public class DeptRelationController {

	@Autowired private TeacherBiz teacherBiz;
	@Autowired private DeptRelationBiz deptRelationBiz;
	@Autowired private DeptBiz deptBiz;
	
	@RequestMapping("/teacherListData")
	@ResponseBody
	public String teacherListData(CommonPage commonPage) {
		teacherBiz.queryTeacherByDept(commonPage);
		return commonPage.toSHowPage();
	}
	
	@RequestMapping("/delete")
	public @ResponseBody String delete(@RequestParam(value = "teacher") int[] teacher,@RequestParam(value="deptId") Integer deptId) {
		List<Integer> ids = new ArrayList<Integer>();
		for (int i = 0; i < teacher.length; i++) {
			ids.add(teacher[i]);
		}
		return JSONObject.toJSONString(deptRelationBiz.removeDeptRelationList(deptId, ids));
	}
	
	@RequestMapping(value = "/teacherAdd")
	public ModelAndView teacherAdd(@RequestParam(value = "deptId") Integer deptId){
		ModelAndView view = new ModelAndView("ss/dept/deptAddTeacher");
		DeptBean dept = deptBiz.queryDeptByDeptId(deptId);
		view.addObject("dept", dept);
		List<Integer> ids = new ArrayList<Integer>();
		DeptRelationBean bean = new DeptRelationBean();
		bean.setDeptId(deptId);
		List<DeptRelationBean> deptrelationlist = deptRelationBiz.queryDeptRelationList(bean);
		List<TeacherBean> teacherlist = null;
		if(deptrelationlist != null){
			for (DeptRelationBean deptRelationBean : deptrelationlist) {
				ids.add(deptRelationBean.getTeacherId());
			}
			teacherlist = teacherBiz.queryTeacherByNotInIdList(ids);
		}else{
			teacherlist = teacherBiz.queryAllTeacher();
		}
		view.addObject("teacherlist", teacherlist);
		return view;
	}
	
	@RequestMapping(value = "/add")
    public @ResponseBody String add(DeptRelationBean bean,int[] teacherId) {
		String deptUrl = bean.getDeptUrl()+";"+bean.getDeptId()+";";
		if(teacherId.length>1){
			List<DeptRelationBean> beanlist = new ArrayList<DeptRelationBean>();
			for (int i = 0; i < teacherId.length; i++) {
				DeptRelationBean deptRelationBean = new DeptRelationBean();
				deptRelationBean.setDeptId(bean.getDeptId());
				deptRelationBean.setDeptUrl(deptUrl);
				deptRelationBean.setTeacherId(teacherId[i]);
				beanlist.add(deptRelationBean);
			}
			return JSONObject.toJSONString(deptRelationBiz.addDeptRelationList(beanlist));
		}
		bean.setDeptUrl(deptUrl);
        return JSONObject.toJSONString(deptRelationBiz.addDeptRelation(bean));
    }
}
